import { defineStore } from 'pinia';
import $router from '@/sheep/router';
import user from './user';
import sys from './sys';

const app = defineStore({
  id: 'app',
  state: () => ({
    info: {
      // 应用信息
      name: '', // 商城名称
      logo: '', // logo
      version: '', // 版本号
      cdnurl: '', // 云存储域名
      filesystem: '', // 云存储平台
      user_protocol: {}, // 用户协议
      privacy_protocol: {}, // 隐私协议
      about_us: {}, // 关于我们
      copyright: '', // 版权信息 I
      copytime: '', // 版权信息 II
    },
    platform: {
      payment: [], // 支持的支付方式
      //recharge_payment: [], // 支持的充值支付方式
      share: {
        methods: [], // 支持的分享方式
        forwardInfo: {}, // 默认转发信息
        posterInfo: {}, // 海报信息
        linkAddress: '', // 复制链接地址
      },
      auto_login: 0, // 自动登陆
      bind_mobile: 0, // 登陆后绑定手机号提醒 (弱提醒，可手动关闭)
    },
    chat: {},
    template: {
      // 店铺装修模板
      basic: {}, // 基本信息
      home: {
        // 首页模板
        style: {},
        data: [],
      },
      user: {
        // 个人中心模板
        style: {},
        data: [],
      },
    },
    shareInfo: {}, // 全局分享信息
    authInfo: null,
  }),
  actions: {
    // 获取Shopro应用配置和模板
    async init(templateId = null) {
      //检查网络
      // const networkStatus = await $platform.checkNetwork();
      // if (!networkStatus) {
      //   $router.error('NetworkError');
      // }
      const res = {
        code: 200,
        msg: '初始化',
        data: {
          app: {
            name: 'Shopro',
            logo: '/static/img/shop/logo.png',
            cdnurl: 'https://ruoyi-mall-sz.oss-cn-shenzhen.aliyuncs.com',
            filesystem: 'qcloud',
            version: '1.1.13',
            user_protocol: {
              title: '用户协议',
              id: '1',
            },
            privacy_protocol: {
              title: '隐私协议',
              id: '2',
            },
            about_us: {
              title: '关于我们',
              id: '3',
            },
            copyright: 'XXX有限公司版权所有',
            copytime: 'Copyright© 2018-2030',
          },
          platform: {
            auto_login: 0,
            bind_mobile: 0,
            payment: ['wechat'],
            //recharge_payment: ["wechat", "alipay"],
            share: {
              methods: ['poster', 'link'],
              linkAddress: 'https://mall.ichengle.top/uni/#/',
              posterInfo: {
                user_bg: '/static/img/shop/config/user-poster-bg.png',
                goods_bg: '/static/img/shop/config/goods-poster-bg.png',
                groupon_bg: '/static/img/shop/config/groupon-poster-bg.png',
              },
            },
          },
          template: {
            basic: {
              tabbar: {
                mode: 1,
                layout: 1,
                inactiveColor: '#8C8C8C',
                activeColor: '#FF7A0C',
                list: [
                  {
                    inactiveIcon: '/icons/home.png',
                    activeIcon: '/icons/home.gif',
                    url: '/pages/index/index',
                    text: '首页',
                  },
                  {
                    inactiveIcon: '/icons/cart.png',
                    activeIcon: '/icons/cart.gif',
                    url: '/pages/index/cart',
                    text: '购物车',
                  },
                  {
                    inactiveIcon: '/icons/my.png',
                    activeIcon: '/icons/my.gif',
                    url: '/pages/index/user',
                    text: '我的',
                  },
                ],
                background: {
                  type: 'color',
                  bgImage: '',
                  bgColor: '#FFFFFF',
                },
              },
              theme: 'orange',
            },
            home: {
              data: [],
              style: {
                background: {
                  color: '#FF9237',
                  src: '',
                },
                navbar: {
                  mode: 'inner',
                  alwaysShow: 1,
                  type: 'color',
                  color: '#EB3924',
                  src: '',
                  list: {
                    mp: [
                      {
                        width: 5,
                        height: 1,
                        top: 0,
                        left: 0,
                        type: 'search',
                        text: '',
                        textColor: '#111111',
                        src: '',
                        url: '',
                        placeholder: '请输入关键字',
                        borderRadius: 20,
                      },
                      {
                        width: 1,
                        height: 1,
                        top: 0,
                        left: 5,
                        type: 'image',
                        text: '',
                        textColor: '#111111',
                        src: '/static/quan.png',
                        url: '/pages/coupon/list',
                        placeholder: '',
                        borderRadius: 0,
                      },
                    ],
                    app: [
                      {
                        width: 7,
                        height: 1,
                        top: 0,
                        left: 0,
                        type: 'search',
                        text: '',
                        textColor: '#111111',
                        src: '',
                        url: '',
                        placeholder: '请输入关键字',
                        borderRadius: 23,
                      },
                      {
                        width: 1,
                        height: 1,
                        top: 0,
                        left: 7,
                        type: 'image',
                        text: '',
                        textColor: '#111111',
                        src: '/static/quan.png',
                        url: '/pages/coupon/list',
                        placeholder: '',
                        borderRadius: 0,
                      },
                    ],
                  },
                },
              },
            },
            user: {
              data: [
                {
                  type: 'userCard',
                  style: {
                    marginLeft: 0,
                    marginRight: 0,
                    marginTop: 70,
                    marginBottom: 10,
                    borderRadiusTop: 0,
                    borderRadiusBottom: 0,
                    background: {
                      type: 'color',
                      bgImage: '',
                      bgColor: '',
                    },
                  },
                },
                {
                  type: 'orderCard',
                  style: {
                    background: {
                      type: 'color',
                      bgImage: '',
                      bgColor: '#FFFFFF',
                    },
                    marginLeft: 10,
                    marginRight: 10,
                    marginTop: 0,
                    marginBottom: 8,
                    borderRadiusTop: 8,
                    borderRadiusBottom: 8,
                    padding: 0,
                  },
                },
                {
                  type: 'couponCard',
                  style: {
                    background: {
                      type: 'color',
                      bgImage: '',
                      bgColor: '#FFFFFF',
                    },
                    marginLeft: 10,
                    marginRight: 10,
                    marginTop: 0,
                    marginBottom: 8,
                    borderRadiusTop: 8,
                    borderRadiusBottom: 8,
                    padding: 0,
                  },
                },
                {
                  type: 'menuGrid',
                  data: {
                    col: 4,
                    list: [
                      {
                        src: '/icons/set.png',
                        title: {
                          text: '清空缓存',
                          color: '#333',
                        },
                        tip: {
                          text: '',
                          color: '#bbb',
                        },
                        url: '/pages/public/setting',
                        badge: {
                          show: 0,
                          text: '',
                          color: '#FFFFFF',
                          bgColor: '#FF6000',
                        },
                        finish: true,
                      },
                      {
                        src: '/icons/edit.png',
                        title: {
                          text: '意见反馈',
                          color: '#333',
                        },
                        tip: {
                          text: '',
                          color: '#bbb',
                        },
                        url: '/pages/public/feedback',
                        badge: {
                          show: 0,
                          text: '',
                          color: '#FFFFFF',
                          bgColor: '#FF6000',
                        },
                        finish: true,
                      },
                      {
                        src: '/icons/message.png',
                        title: {
                          text: '常见问题',
                          color: '#333',
                        },
                        tip: {
                          text: '',
                          color: '#bbb',
                        },
                        url: '/pages/public/richtext?key=mall.question&title=常见问题',
                        badge: {
                          show: 0,
                          text: '',
                          color: '#FFFFFF',
                          bgColor: '#FF6000',
                        },
                        finish: true,
                      },
                      {
                        src: '/icons/userStar.png',
                        title: {
                          text: '关于我们',
                          color: '#333',
                        },
                        tip: {
                          text: '',
                          color: '#bbb',
                        },
                        url: '/pages/public/richtext?key=mall.aboutUs&title=关于我们',
                        badge: {
                          show: 0,
                          text: '',
                          color: '#FFFFFF',
                          bgColor: '#FF6000',
                        },
                        finish: true,
                      },
                      {
                        src: '/icons/privacy.png',
                        title: {
                          text: '隐私协议',
                          color: '#333',
                        },
                        tip: {
                          text: '',
                          color: '#bbb',
                        },
                        url: '/pages/public/richtext?key=mall.privacyAgreement&title=隐私协议',
                        badge: {
                          show: 0,
                          text: '',
                          color: '#FFFFFF',
                          bgColor: '#FF6000',
                        },
                        finish: true,
                      },
                      {
                        src: '/icons/location.png',
                        title: {
                          text: '收货地址',
                          color: '#333',
                        },
                        tip: {
                          text: '',
                          color: '#bbb',
                        },
                        url: '/pages/user/address/list',
                        badge: {
                          show: 0,
                          text: '',
                          color: '#FFFFFF',
                          bgColor: '#FF6000',
                        },
                        finish: true,
                      },
                      {
                        src: '/icons/customer_service.png',
                        title: {
                          text: '联系客服',
                          color: '#333',
                        },
                        tip: {
                          text: '',
                          color: '#bbb',
                        },
                        url: '/pages/chat/index',
                        badge: {
                          show: 0,
                          text: '',
                          color: '#FFFFFF',
                          bgColor: '#FF6000',
                        },
                        finish: true,
                      },
                    ],
                  },
                  style: {
                    background: {
                      type: 'color',
                      bgImage: '',
                      bgColor: '#FFFFFF',
                    },
                    marginLeft: 10,
                    marginRight: 10,
                    marginTop: 0,
                    marginBottom: 10,
                    borderRadiusTop: 8,
                    borderRadiusBottom: 8,
                    padding: 0,
                  },
                },
              ],
              style: {
                background: {
                  color: '#F6F6F6',
                  src: '/icons/b530150a466c8cda0a4cd5b29e2c8d11.png',
                },
              },
            },
          },
          chat: {
            room_id: 'admin',
            chat_domain: 'https://api.shopro.sheepjs.com/chat',
          },
        },
      };
      if (res.code === 200) {
        this.info = res.data.app;
        this.platform = res.data.platform;
        this.template = res.data.template;
        if (!res.data.template) {
          $router.error('TemplateError');
        }
        this.chat = res.data.chat;

        // 加载主题
        const sysStore = sys();
        sysStore.setTheme();

        // 模拟用户登录
        const userStore = user();
        if (userStore.isLogin) {
          userStore.loginAfter();
        }
        return Promise.resolve(true);
      } else {
        $router.error('InitError', res.msg || '加载失败');
      }
    },
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'app-store',
      },
    ],
  },
});

export default app;
